home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SOURCE.ZIP / TSR.ASM < prev    next >
Assembly Source File  |  1986-11-22  |  10KB  |  307 lines

  1.  
  2. ; NOTE : This template is for .COM files only do not use for .EXE files!!
  3.  
  4.  
  5. ;
  6. ;
  7. ;
  8. ;       Copyright 1986 by Dana Nowell - All rights reserved
  9. ;
  10. ; HISTORY:
  11. ;     Version     Date         Name            Description
  12. ;       1.0     11/10/86        dn      first cut
  13. ;       1.01    11/21/86        dn      Fixed memory allocation bug
  14. ;                                       Added installation message
  15. ;
  16.  
  17.  
  18. title   TSR Template
  19.  
  20.  
  21.  
  22.         NULL            equ     00h
  23.         BELL            equ     07h             ; bell character
  24.         BACKSPACE       equ     08h             ; backspace character
  25.         TAB             equ     09h             ; tab character
  26.         LF              equ     0ah             ; line feed
  27.         F_FEED          equ     0ch             ; form feed
  28.         CR              equ     0dh             ; carriage return
  29.         EOF             equ     1ah             ; ctrl z ( end of file )
  30.         SPACE           equ     ' '             ; ascii space character
  31.         QUOTE           equ     '"'
  32.  
  33. SIGNATURE1              equ     6144h           ; used for already
  34. SIGNATURE2              equ     616eh           ; resident check
  35.  
  36. DOS_INT                 equ     21h      ; DOS function interrupt
  37. DISP_CHAR               equ     02h
  38. GET_KEY                 equ     08h
  39. DOS_SCR_MSG             equ     09h
  40. DOS_SET_INT             equ     25h
  41. DOS_RESIDENT            equ     31h
  42. DOS_GET_INT             equ     35h
  43. DOS_TERMINATE           equ     4ch
  44. DOS_STRING_TERM         equ     '$'
  45.  
  46. ; Interrupt vectors used
  47.  
  48. HOOK_INT       equ   1ch      ; interrupt to be hooked ( timer tick now )
  49.  
  50. ;------------------------------------------------------------------------------
  51. ;
  52. ;       MACRO   SECTION
  53. ;
  54. ;------------------------------------------------------------------------------
  55.  
  56. Version_msg     macro
  57.                 jmp     short copyright_end
  58.  
  59. copyright_msg   db      CR, LF
  60.                 db      'TSR Shell - Version 1.01', CR, LF
  61.                 db      'Copyright 1986, Dana Nowell  ', CR, LF, CR, LF
  62.                 db      'May be distributed without license', CR, LF, '$'
  63. copyright_end:
  64.                 Msg     copyright_msg
  65.                 endm
  66.  
  67.  
  68. Msg     macro   ptr
  69.  
  70.         push    dx
  71.         push    ax
  72.  
  73.         lea     dx, ptr
  74.         mov     ah, 09h
  75.         int     21h
  76.  
  77.         pop     ax
  78.         pop     dx
  79.  
  80.         endm
  81.  
  82.  
  83.  
  84.  
  85.  
  86. com     segment para public 'code'
  87.         assume cs:com, ds:com, es:com
  88.  
  89. ;------------------------------------------------------------------------------
  90. ;
  91. ;       note:   The PSP occurs at the beginning of the code segment
  92. ;               for all programs.  In COM files the code seg = data seg
  93. ;
  94. ;------------------------------------------------------------------------------
  95.  
  96.         org     0
  97.  
  98. psp_start       dw      ?       ; int 20h - possibly a block for unresolved
  99.                                 ; externals during link ?
  100.  
  101. mem_size        dw      ?       ; size of available memory in paragraphs
  102. filler          db      ?       ; reserved usually zero
  103.  
  104. dos_call        db      ?       ; call
  105.                 dd      ?       ; address of dos function handler
  106.  
  107. term_vector     dd      ?       ; address of dos terminate routine
  108. break_vector    dd      ?       ; address of dos break routine
  109. error_vector    dd      ?       ; address of dos error routine
  110. dos_reserved    db      2 dup(?); reserved by dos
  111. dos_handles     db      20 dup(?) ; file handle array
  112. environ_ptr     dw      ?       ; seg of dos environment ( offset = 0 )
  113. dos_work        db      34 dup(?) ; dos work area
  114.  
  115. int_21h         db      ?       ; int
  116.                 db      ?       ; 21h
  117.                 db      ?       ; retf ( return far )
  118.  
  119. reserved        dw      ?       ; reserved by dos
  120. fcb1_ext        db      7 dup(?) ; fcb # 1 extension
  121. fcb1            db      9 dup(?) ; fcb #1
  122. fcb2_ext        db      7 dup(?) ; fcb # 2 extension
  123. fcb2            db      20 dup(?) ; fcb #2
  124.  
  125. ;
  126. ;    disk transfer area ( dta ) and parameter block occupy the same space
  127. ;
  128. ;
  129. ;dta             db      128 dup(?) ; disk transfer area
  130.  
  131.  
  132.  
  133. param_len       db      ?       ; length of parameter string ( excludes CR )
  134. parameters      db      127 dup(?) ; parameters
  135.  
  136. ;------------------------------------------------------------------------------
  137. ;
  138. ; Note on standard fcb structure :
  139. ;
  140. ;       The standard FCB is larger than the size reserved in the PSP if you
  141. ; intend to use to FCB data from the PSP move it to a different location.
  142. ;
  143. ;
  144. ;               STANDARD STRUCTURE OF A FILE CONTROL BLOCK
  145. ;
  146. ;
  147. ;    extension :
  148. ;            offset  length             description
  149. ;              -7       1       extension active flag ( 0ffh = active )
  150. ;              -6       5       normally unused should be zeros
  151. ;              -1       1       file attribute when extension is active
  152. ;                                      1  . . . . . . . 1   read-only
  153. ;                                      2  . . . . . . 1 .   hidden
  154. ;                                      4  . . . . . 1 . .   system
  155. ;                                      8  . . . . 1 . . .   volume label
  156. ;                                     16  . . . 1 . . . .   subdirectory
  157. ;                                     32  . . 1 . . . . .   archive
  158. ;                                     64  . 1 . . . . . .   unused
  159. ;                                    128  1 . . . . . . .   unused
  160. ;
  161. ;    fcb :
  162. ;            offset  length             description
  163. ;               0       1       special drive number ( 1 byte )
  164. ;                               0 = default
  165. ;                               1 = a:
  166. ;                               2 = b:       etc
  167. ;               1       8       filename or device name
  168. ;               9       3       filename extension
  169. ;              12       2       current block number
  170. ;              14       2       record size
  171. ;              16       4       file size in bytes ( dos dir entry at open )
  172. ;              20       2       file date ( bit coded as in dir )
  173. ;              22      10       dos work area
  174. ;              32       1       current record number ( 0 - 127 )
  175. ;              33       4       random record number
  176. ;
  177. ;------------------------------------------------------------------------------
  178.  
  179.  
  180.  
  181.         org     100h    ; required for COM file ( skips PSP )
  182.  
  183.  
  184. start:
  185.         jmp     install                 ; install the demon
  186.  
  187. ;-------------------------------------------------------------------
  188. ;
  189. ;               resident data structures go here
  190. ;
  191. ;-------------------------------------------------------------------
  192.  
  193.         old_int         dd      0       ; original value of hooked interrupt
  194.         resident1       dw      SIGNATURE1
  195.         resident2       dw      SIGNATURE2
  196.  
  197.  
  198. ;-------------------------------------------------------------------
  199. ;
  200. ;               new interrupt starts here
  201. ;
  202. ;-------------------------------------------------------------------
  203.  
  204. new_int:
  205.         pushf
  206.  
  207.         sti             ; must turn INT on if we're going to use them
  208.  
  209. ;-------------------------------------------------------------------
  210. ;
  211. ;               be well behaved and pass control to original int
  212. ;
  213. ;-------------------------------------------------------------------
  214.  
  215.         popf
  216.         pushf
  217.         call   dword ptr cs:old_int     ; do old interrupt
  218.  
  219.         iret                            ; bye bye
  220.  
  221. ;------------------------------------------------------------------------------
  222. ;
  223. ;       INSTALLATION DATA STRUCTURES AND CODE GO HERE
  224. ;
  225. ; WARNING WARNING WARNING - this area does not exist after installation
  226. ;
  227. ;------------------------------------------------------------------------------
  228.  
  229. last_resident_byte      db      0       ; last resident byte
  230. resident_flag           dw      0       ; am I already resident ? ( 0 = NO )
  231.  
  232. install_msg             db      CR, LF, 'Installation Complete', CR, LF, '$'
  233.  
  234. already_installed_msg   db      CR, LF
  235.                         db      'Already Installed - Installation Aborted'
  236.                         db      CR, LF, '$'
  237.  
  238. install proc    near
  239.  
  240.         Version_msg
  241.  
  242.  
  243.          mov   al, HOOK_INT           ; int to hook
  244.          mov   ah, DOS_GET_INT        ; get int(AL) vector ==> ES+BX
  245.          int   DOS_INT                ; do the int
  246.          lea   si, old_int            ; where to put old timer interrupt vector
  247.          mov   [si], bx               ; save the offset and segment
  248.          mov   2[si], es              ; ( es also used in check resident )
  249.  
  250.          call   check_resident        ; am I already resident ?
  251.  
  252.          cmp    resident_flag, 0
  253.          je     not_resident
  254.  
  255.          Msg    already_installed_msg
  256.  
  257.          mov     ah, DOS_TERMINATE    ; terminate & stay resident
  258.          mov     al, 1                ; return value is 1 (already installed)
  259.          int     DOS_INT              ; bye-bye
  260.  
  261. not_resident:
  262.          mov   dx, offset new_int     ; offset of new timer interrupt
  263.          mov   al, HOOK_INT           ; timer tick
  264.          mov   ah, DOS_SET_INT        ; set int(AL) vector from DS+DX
  265.          int   DOS_INT                ; do the int
  266.  
  267. ; program terminate and stay resident
  268.  
  269.          Msg     install_msg          ; Display the installation message
  270.  
  271.          mov     dx, offset last_resident_byte
  272.  
  273.          mov     cl, 4                ; convert to paragraphs required to
  274.          shr     dx, cl               ; remain resident ( divide by 16 )
  275.          inc     dx                   ; allow for any remainder of division
  276.  
  277.          mov     ah, DOS_RESIDENT     ; terminate & stay resident
  278.          mov     al, 0                ; return value is 0 (good return)
  279.          int     DOS_INT              ; bye-bye
  280.  
  281. install endp
  282.  
  283.  
  284. ;
  285. ;       Check resident procedure
  286. ;               requires es register to contain the segment address of
  287. ;               the current location for the interrupt being hooked.
  288. ;               use the DOS function 35h to obtain this information.
  289. ;
  290.  
  291. check_resident  proc    near
  292.  
  293.          cmp    es:resident1, SIGNATURE1
  294.          jne    not_res
  295.          cmp    es:resident2, SIGNATURE2
  296.          jne    not_res
  297.  
  298.          mov    resident_flag, 1
  299.  
  300. not_res:
  301.         ret
  302.  
  303. check_resident  endp
  304.  
  305. com     ends
  306.         end     start
  307.